using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using SPThumbnailExtender.TimerJob;
using SPThumbnailExtender.Interne;
using Microsoft.Office.Server.Search.Administration;
using System.Collections.Generic;

namespace SPThumbnailExtender.Features.SPThumbnailExtenderTimerJob
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("5c9cf4df-a3db-4451-972a-6eed34431380")]
    public class SPThumbnailExtenderTimerJobEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            DeleteJob(webApp.JobDefinitions);

            ThumbnailExtenderTimerJob ThumbnailJob = new ThumbnailExtenderTimerJob(webApp);

            SPDailySchedule schedule = new SPDailySchedule();
            schedule.BeginHour = 1;
            schedule.BeginMinute = 0;
            schedule.BeginSecond = 0;
            schedule.EndHour = 3;
            schedule.EndMinute = 0;
            schedule.EndSecond = 0;

            ThumbnailJob.Schedule = schedule;

            ThumbnailJob.Update();
            ThumbnailExtenderTimerJobWI ThumbnailJobWI = new ThumbnailExtenderTimerJobWI(webApp);

            SPMinuteSchedule scheduleWI = new SPMinuteSchedule();
            scheduleWI.Interval = 15;

            ThumbnailJobWI.Schedule = scheduleWI;

            ThumbnailJobWI.Update();

            SearchServiceApplication ssa = GetDefaultSearchServiceApplication();
            ContentSource cs = GetContentSource(ssa);
            if (ssa != null)
                Helper.SetParameter("SPThumbnailExtender.ServiceApplication", ssa.Name);
            if (cs != null)
                Helper.SetParameter("SPThumbnailExtender.ContentSource", cs.Name);


        }

        private ContentSource GetContentSource(SearchServiceApplication ssa)
        {
            if (ssa == null)
                return null;
            Content c = new Content(ssa);
            ContentSourceCollection csc = c.ContentSources;
            foreach (ContentSource cs in csc)
            {
                if (cs is SharePointContentSource)
                    return cs;

            }
            return null;

        }

        private SearchServiceApplication GetDefaultSearchServiceApplication()
        {
            SearchServiceApplication searchApplication=null;
            IEnumerable<SPServiceApplicationProxy> proxies = SPServiceApplicationProxyGroup.Default.DefaultProxies;
            foreach (SPServiceApplicationProxy spa in proxies)
                if (spa is SearchServiceApplicationProxy)
                {
                    SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)spa;
                    string ssaName = ssap.GetSearchApplicationName();
                    SearchQueryAndSiteSettingsService settingsProxy = SPFarm.Local.Services.GetValue<SearchQueryAndSiteSettingsService>();
                    searchApplication = settingsProxy.Applications.GetValue<SearchServiceApplication>(ssaName);
                    break;
                }

            return searchApplication;
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);

        }
        private void DeleteJob(SPJobDefinitionCollection jobs)
        {

            foreach (SPJobDefinition job in jobs)
            {

                if (job.Name.Equals(ThumbnailExtenderTimerJob.JobName,

                StringComparison.OrdinalIgnoreCase))
                {

                    job.Delete();

                }

                if (job.Name.Equals(ThumbnailExtenderTimerJobWI.jobName, StringComparison.OrdinalIgnoreCase))
                {
                    job.Delete();
                }

            }

        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
